home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / othergnu / ispell.zoo / lookup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-06  |  2.7 KB  |  124 lines

  1. /* -*- Mode:Text -*- */
  2.  
  3. /*
  4.  * lookup.c - see if a word appears in the dictionary
  5.  *
  6.  * Pace Willisson, 1983
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include "config.h"
  11. #include "ispell.h"
  12.  
  13.  
  14. struct dent *treelookup();
  15. struct dent *hashtbl;
  16. int hashsize;
  17.  
  18. extern char hashname[];
  19.  
  20. static inited = 0;
  21.  
  22. linit ()
  23. {
  24.     int hashfd;
  25.     register int i;
  26.     register struct dent *dp;
  27.  
  28.     if (inited)
  29.         return;
  30.  
  31.     if ((hashfd = open (hashname, 0)) < 0) {
  32.         fprintf (stderr, "can't open %s\r\n", hashname);
  33.         return (-1);
  34.     }
  35.  
  36.     hashsize = read (hashfd, &hashheader, sizeof hashheader);
  37.     if (hashsize == 0) {
  38.         /*
  39.          * Empty file - create an empty dummy table.  We
  40.          * actually have to have one entry since the hash
  41.          * algorithm involves a divide by the table size
  42.          * (actually modulo, but zero is still unacceptable).
  43.          * So we create an entry with a word of all lowercase,
  44.          * which can't match because the comparison string has
  45.          * been converted to uppercase by then.
  46.          */
  47.         close (hashfd);
  48.         hashsize = 1;        /* This prevents divides by zero */
  49.         hashtbl = (struct dent *) calloc (1, sizeof (struct dent));
  50.         if (hashtbl == NULL) {
  51.             (void) fprintf (stderr,
  52.                 "Couldn't allocate space for hash table\n");
  53.             return (-1);
  54.         }
  55.         hashtbl[0].word = "xxxxxxxxxxx";
  56.         hashtbl[0].next = NULL;
  57.         hashtbl[0].keep = 0;
  58.         hashtbl[0].used = 1;
  59.         /* The flag bits don't matter, but calloc cleared them. */
  60.         inited = 1;
  61.         return 0;
  62.     }
  63.     else if (hashsize < 0  ||  hashheader.magic != MAGIC) {
  64.         fprintf (stderr, "Illegal format hash table\r\n");
  65.         return (-1);
  66.     }
  67.     hashstrings = (char *) malloc (hashheader.stringsize);
  68.     hashtbl = (struct dent *) malloc (hashheader.tblsize * sizeof (struct dent));
  69.     if (hashtbl == NULL  ||  hashstrings == NULL) {
  70.         (void) fprintf (stderr,
  71.             "Couldn't allocate space for hash table\n");
  72.         return (-1);
  73.     }
  74.     hashsize = hashheader.tblsize;
  75.  
  76.     read (hashfd, hashstrings, hashheader.stringsize);
  77.     read (hashfd, hashtbl, hashheader.tblsize * sizeof (struct dent));
  78.     close (hashfd);
  79.  
  80.     for (i = hashsize, dp = hashtbl;  --i >= 0;  dp++) {
  81.         dp->word = &hashstrings [ (int)(dp->word) ];
  82.         if (dp->next == (struct dent *) -1)
  83.             dp->next = NULL;
  84.         else
  85.             dp->next = &hashtbl [ (int)(dp->next) ];
  86.     }
  87.  
  88.     inited = 1;
  89.     return (0);
  90. }
  91.  
  92. /* n is length of s */
  93. struct dent *
  94. lookup (s, n, dotree)
  95. register char *s;
  96. {
  97.     int i;
  98.     register struct dent *dp;
  99.     register char *s1, *s2;
  100.  
  101.     dp = &hashtbl [ hash (s, n, hashsize) ];
  102.     for (  ; dp != NULL; dp = dp->next) {
  103.         /* quick strcmp, but only for equality */
  104.         s1 = dp->word;
  105.         s2 = s;
  106.         while (*s1 == *s2++)
  107.             if (*s1++=='\0') {
  108.                 lastdent = dp;
  109.                 return (lastdent);
  110.             }
  111.     }
  112.     if (dotree) {
  113.         i = s[n];
  114.         s[n] = '\0';
  115.         if ((dp = treelookup (s)) != NULL)
  116.             lastdent = dp;
  117.         s[n] = i;
  118.         return dp;
  119.     }
  120.     else
  121.         return NULL;
  122. }
  123.  
  124.